package com.atues.kuangshengjuc.lock8;

import java.util.concurrent.TimeUnit;
//一个静态方法，一个非静态方法；先执行了打电话，因为类模板和对象锁不是同一个
//两个对象 一个静态方法，一个非静态方法 还是限制性打电话；锁不共通
public class Test4 {
    public static void main(String[] args) {
        Phone4 phone = new Phone4();
        Phone4 phone1 = new Phone4();
        new Thread(() -> {
            try {
                phone.sendMsg();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "A").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }


        new Thread(() -> {
            try {
                phone1.call();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "B").start();


    }
}

class Phone4{

        //方法上锁的是对象锁
        public static synchronized void sendMsg() throws Exception {
            System.out.println("ready to send msg");
            TimeUnit.SECONDS.sleep(4);
            System.out.println("发短信");
        }
        public synchronized  void call() throws Exception {
            System.out.println("打电话");
        }
        public void hello() throws Exception {
            System.out.println("hello");
        }

}
